package com.onenine.app.view;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.onenine.common.util.ApUtils;

import java.io.IOException;

/**
 * 本地视频VideoView
 */
public class LocalVideoView extends SurfaceView implements SurfaceHolder.Callback
        , MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {

    private MediaPlayer mMediaPlayer;
    private OnLocalPrepareListener mLocalPrepareListener;
    private OnLocalCompletionListener mLocalCompletionListener;
    private boolean isLoop;

    public interface OnLocalPrepareListener {
        void onPrepare(MediaPlayer mp);
    }

    public interface OnLocalCompletionListener {
        void onCompletion(MediaPlayer mp);
    }

    public LocalVideoView(Context context) {
        super(context);
        initialize();
    }

    public LocalVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public LocalVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    /**
     * 初始化
     */
    private void initialize() {
        getHolder().addCallback(this);
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnCompletionListener(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mMediaPlayer.setDisplay(holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mMediaPlayer.setDisplay(null);
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mMediaPlayer.setLooping(isLoop);
        mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
        if (!mp.isPlaying()) {
            mp.start();
        }
        if (mLocalPrepareListener != null) {
            mLocalPrepareListener.onPrepare(mMediaPlayer);
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (mLocalCompletionListener != null) {
            mLocalCompletionListener.onCompletion(mp);
        }
    }

    /**
     * 播放播放
     *
     * @param path
     * @param isLoop
     */
    public void startVideo(String path, boolean isLoop) {
        try {
            this.isLoop = isLoop;
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(ApUtils.getApp(), Uri.parse(path));
            mMediaPlayer.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 恢复播放
     */
    public void resumeVideo() {
        try {
            if (!mMediaPlayer.isPlaying()) {
                mMediaPlayer.start();
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 中断播放
     */
    public void pauseVideo() {
        try {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

    }

    /**
     * 停止播放
     */
    public void stopVideo() {
        try {
            mMediaPlayer.stop();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放视频
     */
    public void destroyVideo() {
        mMediaPlayer.release();
    }

    /**
     * 预加载监听
     *
     * @param mLocalPrepareListener
     */
    public void setLocalPrepareListener(OnLocalPrepareListener mLocalPrepareListener) {
        this.mLocalPrepareListener = mLocalPrepareListener;
    }

    /**
     * 完成监听
     *
     * @param mLocalCompletionListener
     */
    public void setLocalCompletionListener(OnLocalCompletionListener mLocalCompletionListener) {
        this.mLocalCompletionListener = mLocalCompletionListener;
    }

}